package bysj.dao;


import bysj.domain.Department;
import bysj.domain.GraduateProjectCategory;
import bysj.domain.School;
import bysj.service.SchoolService;
import util.JdbcHelper;

import java.sql.Connection;
import java.sql.PreparedStatement;
import java.sql.ResultSet;
import java.sql.SQLException;
import java.util.Collection;
import java.util.TreeSet;

public final class GraduateProjectCategoryDao {
    //本类的一个对象引用，保存自身对象
    private static GraduateProjectCategoryDao graduateProjectCategoryDao = null;
    private static Collection<GraduateProjectCategory> graduateProjectCategories;

//    static {
//        graduateProjectCategories = new TreeSet<GraduateProjectCategory>();
//        GraduateProjectCategory design = new GraduateProjectCategory(1, "设计", "01", "");
//        graduateProjectCategories.add(design);
//        graduateProjectCategories.add(new GraduateProjectCategory(2, "论文", "02", ""));
//    }

    //私有的构造方法，防止其它类创建它的对象
    private GraduateProjectCategoryDao() {
    }

    //静态方法，返回本类的惟一对象
    public synchronized static GraduateProjectCategoryDao getInstance() {
        return graduateProjectCategoryDao == null ? new GraduateProjectCategoryDao()
                : graduateProjectCategoryDao;
    }

    public static void main(String[] args) {
        GraduateProjectCategoryDao dao = new GraduateProjectCategoryDao();
        Collection<GraduateProjectCategory> graduateProjectCategories = dao
                .findAll();
    }

    public Collection<GraduateProjectCategory> findAll() {
        return GraduateProjectCategoryDao.graduateProjectCategories;
    }

    public GraduateProjectCategory find(Integer id,Connection conn) throws SQLException {
        GraduateProjectCategory desiredGraduateProjectCategory = null;
        String sql_select_id="SELECT*FROM GraduateProjectCategory WHERE id=?";
        PreparedStatement pstmt = conn.prepareStatement(sql_select_id);
        pstmt.setInt(1,id);
        ResultSet resultSet=pstmt.executeQuery();
        if(resultSet.next()){
            String description=resultSet.getString("description");
            String no = resultSet.getString("no");
            String remarks = resultSet.getString("remarks");
            desiredGraduateProjectCategory= new GraduateProjectCategory(id,description,no,remarks);
        }
        JdbcHelper.close(resultSet,pstmt);
        return desiredGraduateProjectCategory;
    }

    public boolean update(GraduateProjectCategory graduateProjectCategory) {
        graduateProjectCategories.remove(graduateProjectCategory);
        return graduateProjectCategories.add(graduateProjectCategory);
    }

    public boolean add(GraduateProjectCategory graduateProjectCategory) {
        return graduateProjectCategories.add(graduateProjectCategory);
    }

    /*public boolean delete(Integer id) {
        GraduateProjectCategory graduateProjectCategory = this.find(id);
        return this.delete(graduateProjectCategory);
    }*/

    public boolean delete(GraduateProjectCategory graduateProjectCategory) {
        return graduateProjectCategories.remove(graduateProjectCategory);
    }
}
